//
//  CZStatusCell.swift
//  swiftweibo
//
//  Created by Apple on 15/11/29.
//  Copyright © 2015年 tzw. All rights reserved.
//

import UIKit
// 定义一个全局的访问属性
let CZStatusCellMargin: CGFloat  = 8
class CZStatusCell: UITableViewCell {
    // MARK: - 属性
    // 配图视图的宽约束
     var pictureViewWidthCon: NSLayoutConstraint?
    // 配图视图的高约束
     var pictureViewHeightCon: NSLayoutConstraint?
    var status: CZStatus?{
        didSet{
            topView.status = status
            // 设置微博内容
            contenLabel.text = status?.text
            // 设置配图视图模型
            pictureView.status = status
            // 获取配图视图的宽高
            let size = pictureView.calcViewSize()
//            print("配图视图的size:\(size)")
            // 设置配图视图的宽高
            pictureViewWidthCon?.constant = size.width
            pictureViewHeightCon?.constant = size.height
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }
    
    // MARK: - 计算行高
    func rowHeight(status:CZStatus)->CGFloat{
        // 把转进来的模型赋值给self.status
        self.status = status
        // 根据新的内容更新约束
        self.layoutIfNeeded()
        
        
        // 获取底部视图的最大值则是cell的高度
        return CGRectGetMaxY(bottomView.frame)
    }
    // 准备UI
     func prepareUI(){
//        bottomView.backgroundColor = UIColor.orangeColor()
        /// 添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contenLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        // 添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        // 内容
        contenLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: CZStatusCellMargin))
        // 内容的宽度
        contentView.addConstraint(NSLayoutConstraint(item: contenLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2*CZStatusCellMargin))
        
//        // 配图视图
//      let con = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contenLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
//        // 获取配图视图的宽高约束
//        pictureViewWidthCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Width)
//        pictureViewHeightCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Height)
        
        
        // 底部
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -CZStatusCellMargin, y: CZStatusCellMargin))
        // 底部view和cell的底部重合
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }


    // MARK: 懒加载
    private lazy var topView: CZStatusTopView = CZStatusTopView()
    // 内容
     lazy var contenLabel: UILabel = {
        
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
        label.numberOfLines = 0
        return label
    }()
    // 底部view
     lazy var bottomView: CZStatusButtomView = CZStatusButtomView()
    // 配图 
    lazy var pictureView: CZPictureView = CZPictureView()

}
